package com.idlecode.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.idlecode.bean.CodeFormatEntity;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.Service;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: IdleCode
 * @description 缓存代码格式配置信息
 * @author: rongjuwei
 * @create: 2024-02-06 09:37
 **/
@Service
@State(name = "IdleCodeCodeFormat", storages = @Storage(value = "IdleCodeCodeFormat.xml"))
public final class CodeFormatService implements PersistentStateComponent<CodeFormatService> {

    private List<CodeFormatEntity> codeFormatEntities;

    public static CodeFormatService getInstance() {
        return ApplicationManager.getApplication().getService(CodeFormatService.class);
    }

    /**
     * 持久化配置
     */
    @Override
    public CodeFormatService getState() {
        return this;
    }

    /**
     * 加载配置
     */
    @Override
    public void loadState(@NotNull CodeFormatService state) {
        this.codeFormatEntities = state.codeFormatEntities != null ? state.codeFormatEntities : new ArrayList<>();
    }

    /**
     * 找不到配置文件时加载
     */
    @Override
    public void noStateLoaded() {
        System.out.println("找不到配置文件");
        codeFormatEntities = new ArrayList<>();
    }

    /**
     * 获取数据库绑定的代码格式
     */
    public String getCodeFormatName(@NotNull String dsId, @NotNull String dataBaseName) {
        return getCodeFormatEntity(dsId).infoGet(dataBaseName);
    }

    /**
     * 获取数据源绑定的配置
     */
    public CodeFormatEntity getCodeFormatEntity(@NotNull String dsId) {
        CodeFormatEntity formatEntity = null;
        if (CollUtil.isNotEmpty(codeFormatEntities)) {
            formatEntity = codeFormatEntities.stream()
                    .filter(codeFormatEntity -> dsId.equals(codeFormatEntity.getDsId())).findFirst().orElse(null);
        }
        if(formatEntity == null){
            formatEntity =  new CodeFormatEntity(dsId);
        }
        return formatEntity;
    }

    /**
     * 暂存数据源配置
     */
    public void setCodeFormatEntity(@NotNull CodeFormatEntity codeFormatEntity) {
        if (StrUtil.isBlank(codeFormatEntity.getDsId())) {
            throw new NullPointerException("dsId is null");
        }
        int index = codeFormatEntities.indexOf(codeFormatEntity);
        if (index == -1) {
            codeFormatEntities.add(codeFormatEntity);
        } else {
            codeFormatEntities.set(index, codeFormatEntity);
        }
    }

    /**
     * 删除数据源配置
     */
    public void deleteCodeFormatEntity(@NotNull String dsId) {
        for (CodeFormatEntity codeFormatEntity : codeFormatEntities) {
            if (dsId.equals(codeFormatEntity.getDsId())) {
                codeFormatEntities.remove(codeFormatEntity);
                break;
            }
        }
    }

    public List<CodeFormatEntity> getCodeFormatEntities() {
        return codeFormatEntities;
    }

    public void setCodeFormatEntities(List<CodeFormatEntity> codeFormatEntities) {
        this.codeFormatEntities = codeFormatEntities;
    }
}
